﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Entities;
/// <summary>
/// Summary description for ProductData
/// </summary>
public class ProductData
{
    ShoppingCartDataContext shop;

    public ProductData()
    {
        shop = new ShoppingCartDataContext();
    }

    public IEnumerable<Product> AllProduct()
    {
        shop = new ShoppingCartDataContext();
        IEnumerable<Product> list = from p in shop.Products
                                    where p.isActive == true
                                    select p;
        return list;

    }

    public IEnumerable<Product> SearchProductByName(string name)
    {
        shop = new ShoppingCartDataContext();
        IEnumerable<Product> list = shop.Products.Where(p => p.ProductName.Contains(name));
        return list;
    }

    public IEnumerable<Product> SearchProductByPrice(decimal price)
    {
        shop = new ShoppingCartDataContext();
        IEnumerable<Product> list = shop.Products.Where(p => p.ProductPrice == price);
        return list;
    }

    public Product GetProductById(int id)
    {
        shop = new ShoppingCartDataContext();
        Product pr = shop.Products.Single(p => p.ProductID == id);
        return pr;
    }

    public bool AddNewProduct(Product pr)
    {
        try
        {
            shop = new ShoppingCartDataContext();
            shop.Products.InsertOnSubmit(pr);
            shop.SubmitChanges();
            return true;
        }
        catch (Exception ex)
        {

            return false;
        }

    }

    public void DeleteProductById(int id)
    {
        shop = new ShoppingCartDataContext();
        Product pr = shop.Products.First(p => p.ProductID == id);
        pr.isActive = false;
        shop.SubmitChanges();
    }

    public void EditProduct(Product pro)
    {
        shop = new ShoppingCartDataContext();
        Product pr = shop.Products.Single(p => p.ProductID == pro.ProductID);
        pr.ProductName = pro.ProductName;
        pr.ProductPrice = pro.ProductPrice;
        pr.Quantity = pro.Quantity;
        pr.SupplierID = pro.SupplierID;
        pr.CategoryID = pro.CategoryID;
        pr.imageLink = pro.imageLink;
        pr.Unit = pro.Unit;
        shop.SubmitChanges();
    }

    public IEnumerable<Product> getProductByCategory(int cid)
    {
        ShoppingCartDataContext ctx = new ShoppingCartDataContext();
        IEnumerable<Product> list = ctx.Products.Where(p => p.CategoryID == cid);
        return list;
    }

    public IEnumerable<Product> getProductBySupplier(int sid)
    {
        ShoppingCartDataContext ctx = new ShoppingCartDataContext();
        IEnumerable<Product> list = ctx.Products.Where(p => p.SupplierID == sid);
        return list;
    }

    public IEnumerable<Product> SearchProductByPrice(int bPrice, int ePrice)
    {
        ShoppingCartDataContext shop = new ShoppingCartDataContext();
        IEnumerable<Product> list = shop.Products.Where(p => p.ProductPrice >= bPrice && p.ProductPrice <= ePrice);
        return list;
    }
}